package com.kch.system.service.impl;

import com.kch.system.domain.SysUser;
import com.kch.system.domain.Train;
import com.kch.system.domain.TrainApplicants;
import com.kch.system.mapper.TrainApplicantsMapper;
import com.kch.system.mapper.TrainMapper;
import com.kch.system.req.CommonReq;
import com.kch.system.service.ITrainApplicantsService;
import com.kch.system.vo.TrainMemberVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class TrainApplicantsServiceImpl implements ITrainApplicantsService {

    @Autowired
    private TrainApplicantsMapper trainApplicantsMapper;
    @Autowired
    private TrainMapper trainMapper;

    @Override
    public List<TrainMemberVO> selectByTrainId(TrainApplicants applicants) {
        return trainApplicantsMapper.selectByTrainId(applicants);
    }

    @Override
    public int updateById(TrainApplicants applicants) {
        //修改报名表审核状态
        TrainApplicants exist = trainApplicantsMapper.selectByPrimaryKey(applicants.getId());
        exist.setUpdateTime(new Date());
        exist.setUpdateBy(applicants.getUpdateBy());
        exist.setReviewStatus(applicants.getReviewStatus());
        //如果是通过，则修改会议的人数
        if("1".equals(applicants.getReviewStatus())){
            Train train = trainMapper.selectByPrimaryKey(exist.getTrainId());
            train.setMemberPass(train.getMemberPass() + 1);
            train.setUpdateTime(new Date());
            train.setUpdateBy(applicants.getUpdateBy());
            trainMapper.updatePassMember(train);
        }
        return trainApplicantsMapper.updateByPrimaryKey(exist);
    }

    @Override
    public Map<Integer, Integer> checkUserReg(List<Integer> trainIds, Integer userId) {
        List<TrainApplicants> trains = trainApplicantsMapper.selectByTrainIds(trainIds, userId);
        return trains.stream().collect(Collectors.toMap(TrainApplicants::getTrainId,TrainApplicants::getUserId, (e1, e2)->e1));
    }

    @Override
    public int application(CommonReq req, SysUser sysUser) {
        Train train = trainMapper.selectByPrimaryKey(req.getId());
        if(ObjectUtils.isEmpty(train)){
            throw new RuntimeException("传入参数有误,对应记录不存在！");
        }
        train.setMemberEntry(train.getMemberEntry() + 1);
        train.setUpdateBy(sysUser.getLoginName());
        train.setUpdateTime(new Date());
        trainMapper.updateEntryMember(train);

        TrainApplicants applicants = new TrainApplicants();
        applicants.setTrainId(req.getId());
        applicants.setUserId(sysUser.getUserId().intValue());
        applicants.setReviewStatus("0");
        applicants.setRegTime(new Date());
        applicants.setCreateBy(sysUser.getLoginName());
        applicants.setCreateTime(new Date());
        return trainApplicantsMapper.insert(applicants);
    }
}
